# -*- coding: utf-8 -*-
# @Time    : 2020/6/15 2:32
# @Author  : dearMrYang
# @File    : forms.py
# @Software: PyCharm
import re

from django import forms
from django.contrib.auth import get_user_model
from django.core.cache import cache

from apps.form import FormMixin


class SigninForm(FormMixin):
    telephone = forms.CharField(
        required=True,
        max_length=11,
        error_messages={
            'required': '请输入手机号码~~',
            'max_length': '手机号码格式错误~~',
        }
    )
    password = forms.CharField(
        required=True,
        max_length=20,
        min_length=6,
        error_messages={
            'required': '请输入密码~~',
            'max_length': '密码最多 20 个字符~~',
            'min_length': '密码最少 6 个字符~~',
        }
    )
    imgcode = forms.CharField(
        required=True,
        max_length=6,
        min_length=6,
        error_messages={
            'required': '请输入图形验证码~~',
            'max_length': '图形验证码错误~~',
            'min_length': '图形验证码错误~~',
        }
    )
    formid = forms.CharField()
    remember = forms.IntegerField(required=False)

    def clean(self):
        cleaned_data = super(SigninForm, self).clean()
        imgcode = cleaned_data.get('imgcode', '').lower()
        formid = cleaned_data.get('formid')
        cache_imgcode = cache.get(f'img_{formid}', '').lower()
        if not all([imgcode, formid, cache_imgcode, cache_imgcode == imgcode]):
            # raise forms.ValidationError(message='验证码错误~~')  # 全局错误 __all__

            # raise forms.ValidationError(message={
            #     'imgcode': [{
            #         'messages': '验证码错误~~',
            #     }],
            # })  # imgcode错误 imgcode
            self.add_error('imgcode', '图形验证码错误~~')  # imgcode错误 imgcode
        return cleaned_data


class SignupForm(FormMixin):
    telephone = forms.CharField(
        required=True,
        max_length=11,
        error_messages={
            'required': '请输入手机号码~~',
            'max_length': '手机号码格式错误~~',
        }
    )
    username = forms.CharField(
        required=True,
        max_length=100,
        min_length=1,
        error_messages={
            'required': '请输入用户名~~',
            'max_length': '用户名长度错误~~',
            'min_length': '用户名长度错误~~',
        }
    )
    password = forms.CharField(
        max_length=20,
        min_length=6,
        error_messages={
            'required': '请输入密码~~',
            'max_length': '密码最多 20 个字符~~',
            'min_length': '密码最少 6 个字符~~',
        }
    )
    smscode = forms.CharField(
        max_length=6,
        min_length=6,
        error_messages={
            'required': '请输入短信验证码~~',
            'max_length': '短信验证码错误~~',
            'min_length': '短信验证码错误~~',
        }
    )

    # 局部钩子
    def clean_telephone(self):
        telephone = self.cleaned_data.get('telephone')
        ret = re.match(r"^(13|14|15|17|18|19)[0-9]{9}$", telephone)
        if not ret:
            raise forms.ValidationError(message='手机号码格式错误~~')  # 直接加入telephone
        model_user = get_user_model()
        user = model_user.objects.filter(telephone=telephone)
        if user.exists():
            raise forms.ValidationError(message='该手机号码已注册~~')
        return telephone

    def clean(self):
        cleaned_data = super(SignupForm, self).clean()
        telephone = cleaned_data.get('telephone', '').lower()
        smscode = cleaned_data.get('smscode', '').lower()
        cache_smscode = cache.get(f'sms_{telephone}', '').lower()
        if not all([telephone, smscode, cache_smscode, cache_smscode == smscode]):
            self.add_error('smscode', '短信验证码错误~~')
        return cleaned_data
